using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using System.Collections.Generic;

namespace IQIGame.Onigao.Logic
{
    public partial class LevelEntity : IConditionDynCfgKeyPropFetcher
    {
        public int FetchConditionProp(ConditionBlackboard blackboard, int iType, IReadOnlyList<int> curValueParams)
        {
            LevelPlayer player = blackboard.curPlayer as LevelPlayer;
            ConditionEntityPropType type = (ConditionEntityPropType)iType;
            switch (type)
            {
                case ConditionEntityPropType.IsHeroInSight:
                    return IsEntityInSight(player.entity) ? 1 : 0;
                case ConditionEntityPropType.IsStoppedElevator:
                    return HasDynamicParam(EntityDynamicParamType.Elevator) && !region.IsRunningElevator(id) ? 1 : 0;
                case ConditionEntityPropType.DistanceToOther:
                    LevelEntity otherEntity = region.GetEntityByTagOrMcid(curValueParams[1]);
                    return otherEntity == null ? int.MaxValue : (int)(otherEntity.position - position).sqrMagnitude;
                case ConditionEntityPropType.ExecCounts:
                    return this.execCount;
                case ConditionEntityPropType.IsObstacleActive:
                    return isObstacleActive ? 1 : 0;
                case ConditionEntityPropType.ElevatorCurFloor:
                    return this.CalcElevatorFloor();
                case ConditionEntityPropType.HaveBuff:
                    int buffCid = curValueParams[1];
                    return buffManager.ContainsBuff(buffCid) ? 1 : 0;
                case ConditionEntityPropType.DistanceToPosition:
                    int x = curValueParams[1];
                    int y = curValueParams[2];
                    int z = curValueParams[3];
                    Vector3Logic targetPos = new Vector3Logic(x, y, z);
                    return (int)(targetPos - position).sqrMagnitude;
                case ConditionEntityPropType.IsNavigating:
                    return this._navigator != null && this.navigator.isNavigating ? 1 : 0;
                case ConditionEntityPropType.IsElevatorFloorActive:
                    int targetFloor = curValueParams[1];
                    var prop = this.GetDynamicProperty(EntityDynamicPropertyType.Elevator) as EntityPropElevator;
                    return prop != null && prop.floorActiveList[targetFloor] ? 1 : 0;
                default:
                    LogicLog.LogError($"关卡逻辑层不支持的条件类型：3 - {iType}");
                    return 0;
            }
        }

        /// <summary>
        /// 实现接口方法：根据动态参数key，获取动态参数中的实体tag或mcid
        /// </summary>
        /// <param name="dynamicKey"></param>
        /// <returns></returns>
        public int GetValueFromDynCfgKey(string key)
        {
            var dynParamDict = entityConfig.serviceDynamicParam;
            if (dynParamDict != null && dynParamDict.TryGetValue(key, out int dynKeyValue))
            {
                return dynKeyValue;
            }
            return -1;
        }
    }
}
